package main

import (
	"github.com/xuri/excelize"
	"fmt"
	"match-spider/util/boltdb"
	"github.com/boltdb/bolt"
	"strings"
	"strconv"
	"encoding/json"
)

/**
导出比赛数据到excel

 */

func europe( fn func(map[string][]string,int))  {
	num:=0
	company:=[]string{
		"澳门","竞彩官方","金宝博(马恩岛)","bet 365(英国)","Oddset(德国)","威廉希尔(英国)","Interwetten","香港马会(中国香港)",
	}
	boltdb.ScanTx("matchs-2016", func(tx *bolt.Tx) {

		tx.ForEach(func(name []byte, b *bolt.Bucket) error {
			b.ForEach(func(k, v []byte) error {
				content:=string(v)
				s:=strings.Split(content,"^")
				league:=s[1]
				if league=="英冠"{

						m:=map[string][]string{
							"match":s,
						}
						boltdb.Get("europe-2016",league,string(k), func(data string) {
							if data!=""{

								num++
								odds:=make([]string,0)
								json.Unmarshal([]byte(data),&odds)
								for _,v:=range odds {
									fmt.Println(v)
									d := strings.Split(v, "|")
									name := d[21]
									slice:=[]string{d[3],d[4],d[5],d[10],d[11],d[12],}
									if strings.Compare(name,"澳门")==0{
										m["澳门"]=slice
									}
									if strings.Compare(name,"竞彩官方")==0{
										m["竞彩官方"]=slice
									}
									if strings.Compare(name,"金宝博(马恩岛)")==0{
										m["金宝博(马恩岛)"]=slice
									}
									if strings.Compare(name,"bet 365(英国)")==0{
										m["bet 365(英国)"]=slice
									}
									if strings.Compare(name,"Oddset(德国)")==0{
										m["Oddset(德国)"]=slice
									}
									if strings.Compare(name,"威廉希尔(英国)")==0{
										m["威廉希尔(英国)"]=slice
									}
									if strings.Compare(name,"Interwetten")==0{
										m["Interwetten"]=slice
									}
									if strings.Compare(name,"香港马会(中国香港)")==0{
										m["香港马会(中国香港)"]=slice
									}

								}
								for _,v:=range company{
									if m[v]==nil{
										m[v]=[]string{
											"","","","","","",
										}
									}
								}
								fn(m,num)
							}

						})


				}

				return nil
			})
			return nil
		})

	})
	fmt.Println(num)
}
func main()  {


	xlsx := excelize.NewFile()

	categories := map[string]string{
		"A1": "编号",
		"B1": "联赛",
		"C1": "时间",
		"D1": "主队",
		"E1": "进球",
		"F1": "客队",
		"G1": "进球",
		"H1": "亚盘",
		"I1": "大小",
		"J1": "澳门",
		"K1": "",
		"L1": "",
		"M1": "",
		"N1": "",
		"O1": "",
		"P1": "竞彩官方",
		"Q1": "",
		"R1": "",
		"S1": "",
		"T1": "",
		"U1": "",
		"V1": "金宝博(马恩岛)",//
		"W1": "",
		"X1": "",
		"Y1": "",
		"Z1": "",
		"AA1": "",
		"AB1": "bet 365(英国)",//
		"AC1": "",
		"AD1": "",
		"AE1": "",
		"AF1": "",
		"AG1": "",
		"AH1": "Oddset(德国)",//
		"AI1": "",
		"AJ1": "",
		"AK1": "",
		"AL1": "",
		"AM1": "",
		"AN1": "威廉希尔(英国)",//
		"AO1": "",
		"AP1": "",
		"AQ1": "",
		"AR1": "",
		"AS1": "",
		"AT1": "Interwetten",//
		"AU1": "",
		"AV1": "",
		"AW1": "",
		"AX1": "",
		"AY1": "",
		"AZ1": "香港马会(中国香港)",//
		"BA1": "",
		"BB1": "",
		"BC1": "",
		"BD1": "",
		"BE1": "",
		/*"BF1": "金宝博",//
		"BG1": "",
		"BH1": "",
		"BI1": "",
		"BJ1": "",
		"BK1": "",
		"BL1": "12bet/大发",//
		"BM1": "",
		"BN1": "",
		"BO1": "",
		"BP1": "",
		"BQ1": "",
		"BR1": "利记",//
		"BS1": "",
		"BT1": "",
		"BU1": "",
		"BV1": "",
		"BW1": "",
		"BX1": "盈禾",//
		"BY1": "",
		"BZ1": "",
		"CA1": "",
		"CB1": "",
		"CC1": "",
		"CD1": "18Bet",//
		"CE1": "",
		"CF1": "",
		"CG1": "",
		"CH1": "",
		"CI1": "",*/
	}

	for k,v:=range categories{
		xlsx.SetCellValue("Sheet1", k, v)
	}
	europe(func(data map[string][]string,i int) {
		match:=data["match"]
		odds1:=data["澳门"]
		odds2:=data["竞彩官方"]
		odds3:=data["金宝博(马恩岛)"]
		odds4:=data["bet 365(英国)"]
		odds5:=data["Oddset(德国)"]
		odds6:=data["威廉希尔(英国)"]
		odds7:=data["Interwetten"]
		odds8:=data["香港马会(中国香港)"]


		cMatch:=[]string{
			"A",
			"B",
			"C",
			"D",
			"E",
			"F",
			"G",
			"H",
			"I",
			"J",
		}
		c1:=[]string{
			"J",
			"K",
			"L",
			"M",
			"N",
			"O",
		}
		c2:=[]string{
			"P",
			"Q",
			"R",
			"S",
			"T",
			"U",
		}
		c3:=[]string{
			"V",
			"W",
			"X",
			"Y",
			"Z",
			"AA",
		}
		c4:=[]string{
			"AB",
			"AC",
			"AD",
			"AE",
			"AF",
			"AG",
		}
		c5:=[]string{
			"AH",
			"AI",
			"AJ",
			"AK",
			"AL",
			"AM",
		}
		c6:=[]string{
			"AN",
			"AO",
			"AP",
			"AQ",
			"AR",
			"AS",
		}
		c7:=[]string{
			"AT",
			"AU",
			"AV",
			"AW",
			"AX",
			"AY",
		}
		c8:=[]string{
			"AZ",
			"BA",
			"BB",
			"BC",
			"BD",
			"BE",
		}


		for k,v:=range match{
			index:=cMatch[k]+strconv.Itoa(i+1)
			xlsx.SetCellValue("Sheet1", index, v)
		}

		for k,v:=range odds1{
			index:=c1[k]+strconv.Itoa(i+1)
			xlsx.SetCellValue("Sheet1", index, v)
		}

		for k,v:=range odds2{
			index:=c2[k]+strconv.Itoa(i+1)
			xlsx.SetCellValue("Sheet1", index, v)
		}

		for k,v:=range odds3{
			index:=c3[k]+strconv.Itoa(i+1)
			xlsx.SetCellValue("Sheet1", index, v)
		}

		for k,v:=range odds4{
			index:=c4[k]+strconv.Itoa(i+1)
			xlsx.SetCellValue("Sheet1", index, v)
		}

		for k,v:=range odds5{
			index:=c5[k]+strconv.Itoa(i+1)
			xlsx.SetCellValue("Sheet1", index, v)
		}

		for k,v:=range odds6{
			index:=c6[k]+strconv.Itoa(i+1)
			xlsx.SetCellValue("Sheet1", index, v)
		}
		for k,v:=range odds7{
			index:=c7[k]+strconv.Itoa(i+1)
			xlsx.SetCellValue("Sheet1", index, v)
		}
		for k,v:=range odds8{
			index:=c8[k]+strconv.Itoa(i+1)
			xlsx.SetCellValue("Sheet1", index, v)
		}

	})
	err := xlsx.SaveAs("./Workbook.xlsx")
	if err != nil {
		fmt.Println(err)
	}
}